package com.lc.ibps.platform.rest.org;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.base.model.PartyEntity;
import com.lc.ibps.api.base.query.QueryFilter;
import com.lc.ibps.api.base.query.QueryOP;
import com.lc.ibps.api.org.service.IPartyOrgService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.BeanUtils;
import com.lc.ibps.base.core.util.string.StringUtil;
import com.lc.ibps.base.db.mybatis.domain.DefaultPage;
import com.lc.ibps.cloud.entity.APIPageList;
import com.lc.ibps.cloud.entity.APIRequest;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.org.party.persistence.entity.PartyOrgPo;
import com.lc.ibps.org.party.repository.PartyOrgRepository;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 组织服务 控制器，第三方系统调用。
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2016年9月22日-下午2:47:07
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/webapi/orgService")
@Api(value = "/orgService", tags = "用户管理-组织服务")
@RestController
public class OrgController extends BaseApiController {

	private IPartyOrgService orgService;
	private PartyOrgRepository partyOrgRepository;

	public OrgController() {
		orgService = AppUtil.getBean(IPartyOrgService.class);
		partyOrgRepository = AppUtil.getBean(PartyOrgRepository.class);
	}

	@ApiOperation(value = "根据ID获取组织", notes = "根据ID获取组织")
	@RequestMapping(value = "/loadById", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadById(
			@ApiParam(value = "id", required = true) @RequestParam(name = "id", required = true) String id) {
		APIResult<PartyEntity> result = new APIResult<>();
		try {
			PartyEntity entity = orgService.getById(id);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据别名获取组织", notes = "根据别名获取组织")
	@RequestMapping(value = "/loadByAlias", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<PartyEntity> result = new APIResult<>();
		try {
			PartyEntity entity = orgService.getByAlias(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据别名获取直接父级组织", notes = "根据别名获取直接父级组织")
	@RequestMapping(value = "/loadDirectParent", method = RequestMethod.GET)
	public APIResult<PartyEntity> loadDirectParent(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<PartyEntity> result = new APIResult<>();
		try {
			PartyEntity entity = orgService.getDirectParent(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织失败！", e);
		}

		return result;
	}

	@ApiOperation(value = "根据别名获取父级组织列表", notes = "根据别名获取父级组织列表")
	@RequestMapping(value = "/findParents", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findParents(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<List<PartyEntity>> result = new APIResult<>();// TODO 有问题 没有获取父级列表 只有父级本身
		try {
			List<PartyEntity> entity = orgService.findParents(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取下属组织列表", notes = "根据别名获取下属组织列表")
	@RequestMapping(value = "/findChildren", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findChildren(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "descendants", required = false, defaultValue = "false") @RequestParam(name = "descendants", required = false) boolean descendants) {
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = orgService.findChildren(alias, descendants);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据组织关系获取组织列表", notes = "根据组织关系获取组织列表")
	@RequestMapping(value = "/findByOrgRelation", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByOrgRelation(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey,
			@ApiParam(value = "relPartyName", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName,
			@ApiParam(value = "relLevel", required = false, defaultValue = "1") @RequestParam(name = "relLevel", required = false) int relLevel,
			@ApiParam(value = "isRecursion", required = false, defaultValue = "false") @RequestParam(name = "isRecursion", required = false) boolean isRecursion) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>();
		try {
			List<PartyEntity> entity = orgService.findByOrgRelation(alias, relTypeKey, relPartyName, relLevel, isRecursion);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户关系获取组织列表", notes = "根据用户关系获取组织列表")
	@RequestMapping(value = "/findByUserRelation", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserRelation(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey) {

		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByUserRelation(account, relTypeKey);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据扩展属性获取组织列表", notes = "根据扩展属性获取组织列表")
	@RequestMapping(value = "/findByAttrKeyValue", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByAttrKeyValue(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByAttrKeyValue(account, relTypeKey);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户账号获取组织列表", notes = "根据用户账号获取组织列表")
	@RequestMapping(value = "/findByUserAccount", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserAccount(
			@ApiParam(value = "account", required = true) @RequestParam(name = "account", required = true) String account) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByUserAccount(account);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据用户ID获取组织列表", notes = "根据用户ID获取组织列表")
	@RequestMapping(value = "/findByUserId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByUserId(
			@ApiParam(value = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByUserId(userId);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据组织关系获取组织列表", notes = "根据组织关系获取组织列表")
	@RequestMapping(value = "/findByOrgRel", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByOrgRel(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "relTypeKey", required = true) @RequestParam(name = "relTypeKey", required = true) String relTypeKey,
			@ApiParam(value = "relPartyName", required = true) @RequestParam(name = "relPartyName", required = true) String relPartyName) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByOrgRel(alias, relTypeKey, relPartyName);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取所有组织列表", notes = "获取所有组织列表")
	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findAll() {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findAll();
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据父ID获取组织列表", notes = "根据父ID获取组织列表")
	@RequestMapping(value = "/queryByParentId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> queryByParentId(
			@ApiParam(value = "parentId", required = true) @RequestParam(name = "parentId", required = true) String parentId,
			@ApiParam(value = "page", required = false, defaultValue = "1") @RequestParam(name = "page", required = false) int page,
			@ApiParam(value = "limit", required = false, defaultValue = "20") @RequestParam(name = "limit", required = false) int limit) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.queryByParentId(parentId, new DefaultPage(page, limit));
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据父ID或名称获取组织列表", notes = "根据父ID或名称获取组织列表")
	@RequestMapping(value = "/queryByParentIdOrName", method = RequestMethod.POST)
	public APIResult<APIPageList<PartyOrgPo>> queryByParentIdOrName(
			@ApiParam(name = "request", value = "传入查询请求json字符串", required = true) @RequestBody(required = true) APIRequest request) {

		APIResult<APIPageList<PartyOrgPo>> result = new APIResult<>();
		try {
			List<PartyOrgPo> entity = null;
			QueryFilter queryFilter = getQuerFilter(request);
			if (StringUtil.isNotEmpty(request.getParameter("name"))) {
				queryFilter.addFilter("NAME_", "%" + request.getParameter("name") + "%", QueryOP.LIKE);
				entity = partyOrgRepository.query(queryFilter);
			} else {
				queryFilter.addFilter("ID_", request.getParameter("parentId"), QueryOP.EQUAL);
				entity = partyOrgRepository.queryByParentId(request.getParameter("parentId"), queryFilter.getPage());
			}
//			List<PartyOrgPo> list = new ArrayList<PartyOrgPo>();
			for (PartyEntity partyEntity : entity) {
				PartyOrgPo po = (PartyOrgPo) partyEntity;
				List<PartyOrgPo> l = partyOrgRepository.findByParentId(po.getId());
				if (BeanUtils.isNotEmpty(l)) {
					po.setHasChild(true);
				} else {
					po.setHasChild(false);
				}
//				list.add(po);
			}
			APIPageList<PartyOrgPo> pageList = getAPIPageList(entity);
			result.setData(pageList);
			result.setMessage("获取组织列表数据成功");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表数据失败", e);
		}

		return result;
	}

	@ApiOperation(value = "根据父ID获取所有下属组织列表", notes = "根据父ID获取所有下属组织列表")
	@RequestMapping(value = "/findSubByParentId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findSubByParentId(
			@ApiParam(value = "parentId", required = true) @RequestParam(name = "parentId", required = true) String parentId) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findSubByParentId(parentId);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据父ID获取下属组织列表", notes = "根据父ID获取下属组织列表")
	@RequestMapping(value = "/findWithSelfByParentId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findWithSelfByParentId(
			@ApiParam(value = "parentId", required = true) @RequestParam(name = "parentId", required = true) String parentId) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findWithSelfByParentId(parentId);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据父ID获取下属组织列表", notes = "根据父ID获取下属组织列表")
	@RequestMapping(value = "/findSubWithSelfByParentId", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findSubWithSelfByParentId(
			@ApiParam(value = "parentId", required = true) @RequestParam(name = "parentId", required = true) String parentId) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findSubWithSelfByParentId(parentId);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取下属组织列表", notes = "根据别名获取下属组织列表")
	@RequestMapping(value = "/queryByAlias", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> queryByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias,
			@ApiParam(value = "page", required = false, defaultValue = "1") @RequestParam(name = "page", required = false) int page,
			@ApiParam(value = "limit", required = false, defaultValue = "20") @RequestParam(name = "limit", required = false) int limit) {
		
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.queryByAlias(alias, new DefaultPage(page, limit));
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取下属组织列表", notes = "根据别名获取下属组织列表")
	@RequestMapping(value = "/findByAlias", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findByAlias(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取所有下属组织列表", notes = "根据别名获取所有下属组织列表")
	@RequestMapping(value = "/findSubByAlias", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findSubByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findSubByAlias(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取所有下属组织列表", notes = "根据别名获取所有下属组织列表")
	@RequestMapping(value = "/findWithSelfByAlias", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findWithSelfByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findWithSelfByAlias(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "根据别名获取所有下属组织列表", notes = "根据别名获取所有下属组织列表")
	@RequestMapping(value = "/findSubWithSelfByAlias", method = RequestMethod.GET)
	public APIResult<List<PartyEntity>> findSubWithSelfByAlias(
			@ApiParam(value = "alias", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<List<PartyEntity>> result = new APIResult<>(); 
		try {
			List<PartyEntity> entity = orgService.findSubWithSelfByAlias(alias);
			result.setData(entity);
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取组织列表失败！", e);
		}
		return result;
	}

}
